﻿#include "SaveDevToModelDlg.h"
#include "ui_SaveDevToModelDlg.h"
#include <QMessageBox>

SaveDevToModelDlg::SaveDevToModelDlg(int AppType, int TableId, unsigned int RecordId, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SaveDevToModelDlg)
{
	m_AppType = 0;
	m_TableId = 0;
	m_RecordId = 0;
	m_YxNum = 0;
	m_YcNum = 0;
	m_YkNum = 0;
	m_YmNum = 0;
	m_SetNum = 0;
	m_columnNum = 0;
	m_RecordLength = 0;
	m_pColumnInfo = NULL;
	m_pDataOffset = NULL;
    ui->setupUi(this);
	m_AppType = AppType;
	m_TableId = TableId;
	m_RecordId = RecordId;

	connect(ui->pAffirmBtn, SIGNAL(clicked()), this, SLOT(AffirmBtn()));
	connect(ui->pCancelBtn, SIGNAL(clicked()), this, SLOT(close()));
	InitShow();
}

SaveDevToModelDlg::~SaveDevToModelDlg()
{
    delete ui;
}

void SaveDevToModelDlg::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void SaveDevToModelDlg::InitShow()
{
	if (SYSTEM_TYPE_NSA_YD == m_AppType && 
		TABLE_RDEVS_INFO == m_TableId &&
		m_RecordId >0)
	{
		char	*buff = NULL;
		int	buff_len = 0;
		int	RetVal = 0;
		RetVal = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_RecordId(m_AppType, m_TableId, m_RecordId, buff, &buff_len);
		if (RetVal>0 && buff)
		{
			m_DevName = QString::fromLocal8Bit(((rDevS_Info*)buff)->Name);
		}
		if (buff)
		{
			delete [](char*)buff;
			buff = NULL;
		}
		ui->pSeltDevNameEdit->setText(m_DevName);
	}
}

//确认
void SaveDevToModelDlg::AffirmBtn()
{
	QString ModelName;
	ModelName = ui->pModelNameEdit->text();
	if (ModelName.length()<=0)
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("未输入设备模板名称！"), QMessageBox::Ok | QMessageBox::Cancel);
		return ;
	}
	if (ModelName.length() > STRING_NORMAL_LEN)
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("设备模板名称过长！"), QMessageBox::Ok | QMessageBox::Cancel);
		return ;
	}

	InsertDevModel(ModelName);

	return accept();
}

void SaveDevToModelDlg::ReleasColumnInfo()
{
	if (m_pColumnInfo)
	{
		delete [](char*)m_pColumnInfo;
		m_pColumnInfo = NULL;
	}
	if (m_pDataOffset)
	{
		delete [](int*)m_pDataOffset;
		m_pDataOffset = NULL;
	}
	if (m_columnNum>0)
	{
		m_columnNum = 0;
	}
	if (m_RecordLength>0)
	{
		m_RecordLength = 0;
	}
}

void SaveDevToModelDlg::InsertDevModel(QString ModeName)
{
	if (ModeName.length()<=0 || ModeName.length() > STRING_NORMAL_LEN)
	{
		return ;
	}
	if (SYSTEM_TYPE_NSA_YD != m_AppType && 
		TABLE_RDEVS_INFO != m_TableId &&
		m_RecordId <=0)
	{
		return ;
	}
	rDevModel_Info		rDevMode;
	unsigned int		RecordId = 0;
	int		ModelYxNum = 0;
	int		ModelYcNum = 0;
	int		ModelYkNum = 0;
	int		ModelYmNum = 0;
	int		ModelRybNum = 0;
	g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, RecordId);
	if (RecordId<=0)
	{
		return ;
	}
	
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, m_RecordId+PARENT_OFFSET*TABLE_RDEVS_INFO, &m_YxNum);
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, m_RecordId+PARENT_OFFSET*TABLE_RDEVS_INFO, &m_YcNum);
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, m_RecordId+PARENT_OFFSET*TABLE_RDEVS_INFO, &m_YkNum);
	g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YM, m_RecordId+PARENT_OFFSET*TABLE_RDEVS_INFO, &m_YmNum);

//	g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_RYB, m_RecordId+PARENT_OFFSET*TABLE_RDEVS_INFO, &m_RybNum);
//	g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_BHYC, m_RecordId+PARENT_OFFSET*TABLE_RDEVS_INFO, &m_BhycNum);
	rDevMode.ID = RecordId;
	sprintf(rDevMode.Name, "%s", ModeName.toLocal8Bit().data());

	rDevMode.YxNum = m_YxNum;
	rDevMode.YcNum = m_YcNum;
	rDevMode.YkNum = m_YkNum;
	rDevMode.YmNum = m_YmNum;
//	rDevMode.RybNum = m_RybNum;
//	rDevMode.BhycNum = m_BhycNum;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return ;
	}

	Ret = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, (char*)&rDevMode, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
	if (Ret<=0)
	{
		ReleasColumnInfo();
		return;
	}
	g_pSqLiteDB.BeginTransaction();
	ModelYxNum = InsertYxModel(rDevMode.ID);
	ModelYcNum = InsertYcModel(rDevMode.ID);
	ModelYkNum = InsertYkModel(rDevMode.ID);
	ModelYmNum = InsertYmModel(rDevMode.ID);
	g_pSqLiteDB.CommitTransaction();
	QString Str;
	Str = QString::fromLocal8Bit("保存设备模板“%1”: \r\n YxNum:%2\r\n YcNum:%3\r\n YkNum:%4\r\n YmNum:%5")
		.arg(QString::fromLocal8Bit(rDevMode.Name))
		.arg(ModelYxNum)
		.arg(ModelYcNum)
		.arg(ModelYkNum)
		.arg(ModelYmNum);
	QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), Str, QMessageBox::Ok | QMessageBox::Cancel);
	ReleasColumnInfo();
	emit UpDateTreeItem_Signal(rDevMode.ID);
	return ;
}

int SaveDevToModelDlg::InsertYxModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char	*buff = NULL;
	int	buff_len = 0;
	int	YxNum = 0;
	int	i = 0;
	unsigned int	RecordId = 0;
	rDevModel_Yx DevModeYx;
	QString		 NameCh;
	int		Retval = 0;
	int		DevModexYxNum = 0;
	ReleasColumnInfo();

	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	YxNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(m_AppType, TABLE_RDEVS_YX, m_RecordId+PARENT_OFFSET*TABLE_RDEVS_INFO, buff, &buff_len, true);
	if (YxNum>0 && buff)
	{
		for (i=0; i<YxNum; i++)
		{
			DevModeYx.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			DevModeYx.IndexNo = ((rDevS_Yx*)buff)[i].IndexNo;
			NameCh = QString::fromLocal8Bit(((rDevS_Yx*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(DevModeYx.Name, "%s", NameCh.toLocal8Bit().data());
			DevModeYx.Fun = ((rDevS_Yx*)buff)[i].m_fun;
			DevModeYx.Inf = ((rDevS_Yx*)buff)[i].m_inf;
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			DevModeYx.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX, (char*)&DevModeYx, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexYxNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexYxNum;
}

int SaveDevToModelDlg::InsertYcModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char	*buff = NULL;
	int	buff_len = 0;
	int	YcNum = 0;
	int	i = 0;
	unsigned int	RecordId = 0;
	rDevModel_Yc DevModeYc;
	QString		 NameCh;
	int		Retval = 0;
	int		DevModexYcNum = 0;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	YcNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(m_AppType, TABLE_RDEVS_YC, m_RecordId+PARENT_OFFSET*TABLE_RDEVS_INFO, buff, &buff_len, true);
	if (YcNum>0 && buff)
	{
		for (i=0; i<YcNum; i++)
		{
			DevModeYc.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			DevModeYc.IndexNo = ((rDevS_Yc*)buff)[i].IndexNo;
			NameCh = QString::fromLocal8Bit(((rDevS_Yc*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(DevModeYc.Name, "%s", NameCh.toLocal8Bit().data());
			DevModeYc.Fun = ((rDevS_Yc*)buff)[i].m_fun;
			DevModeYc.Inf = ((rDevS_Yc*)buff)[i].m_inf;
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			DevModeYc.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC, (char*)&DevModeYc, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexYcNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexYcNum;
}

int SaveDevToModelDlg::InsertYkModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char		 *buff = NULL;
	int		 buff_len = 0;
	int		 YkNum = 0;
	int		 i = 0;
	unsigned int		 RecordId = 0;
	rDevModel_Yk DevModeYk;
	QString		 NameCh;
	int		 Retval = 0;
	int		 DevModexYkNum = 0;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	YkNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(m_AppType, TABLE_RDEVS_YK, m_RecordId+PARENT_OFFSET*TABLE_RDEVS_INFO, buff, &buff_len, true);
	if (YkNum>0 && buff)
	{
		for (i=0; i<YkNum; i++)
		{
			DevModeYk.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			DevModeYk.IndexNo = ((rDevS_Yk*)buff)[i].IndexNo;
			NameCh = QString::fromLocal8Bit(((rDevS_Yk*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(DevModeYk.Name, "%s", NameCh.toLocal8Bit().data());
			DevModeYk.Fun = ((rDevS_Yk*)buff)[i].m_fun;
			DevModeYk.Inf = ((rDevS_Yk*)buff)[i].m_inf;
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			DevModeYk.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK, (char*)&DevModeYk, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexYkNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexYkNum;
}

int SaveDevToModelDlg::InsertYmModel(unsigned int DevModelId)
{
	if (DevModelId<=0)
	{
		return 0;
	}

	char		 *buff = NULL;
	int		 buff_len = 0;
	int		 YmNum = 0;
	int		 i = 0;
	unsigned int		 RecordId = 0;
	rDevModel_Ym DevModeYm;
	QString		 NameCh;
	int		 Retval = 0;
	int		 DevModexYmNum = 0;
	ReleasColumnInfo();
	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
	{
		ReleasColumnInfo();
		return 0;
	}
	YmNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(m_AppType, TABLE_RDEVS_YM, m_RecordId+PARENT_OFFSET*TABLE_RDEVS_INFO, buff, &buff_len, true);
	if (YmNum>0 && buff)
	{
		for (i=0; i<YmNum; i++)
		{
			DevModeYm.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			DevModeYm.IndexNo = ((rDevS_Ym*)buff)[i].IndexNo;
			NameCh = QString::fromLocal8Bit(((rDevS_Ym*)buff)[i].Name);
			NameCh.remove(m_DevName);
			sprintf(DevModeYm.Name, "%s", NameCh.toLocal8Bit().data());
			DevModeYm.Fun = ((rDevS_Ym*)buff)[i].m_fun;
			DevModeYm.Inf = ((rDevS_Ym*)buff)[i].m_inf;
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM, RecordId);
			if (RecordId<=0)
			{
				break;
			}

			DevModeYm.ID = RecordId;
			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM, (char*)&DevModeYm, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
			if (Retval<=0)
			{
				break;
			}
			DevModexYmNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}

	ReleasColumnInfo();
	return DevModexYmNum;
}

// int SaveDevToModelDlg::InsertRlySOEModel(unsigned int DevModelId)
// {
// 	if (DevModelId<=0)
// 	{
// 		return 0;
// 	}
// 	char		 *buff = NULL;
// 	int		 buff_len = 0;
// 	int		 RlySOENum = 0;
// 	int		 i = 0;
// 	unsigned int		 RecordId = 0;
// 	rDevModel_RlySOE61850 DevModeRlySOE;
// 	QString		 NameCh;
// 	int		 Retval = 0;
// 	int		 DevModexRlySOENum = 0;
// 	ReleasColumnInfo();
// 	int Ret = g_DbCfgFunObj.GetCurTableColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_RLYSOE61850, (char*&)m_pColumnInfo, m_pDataOffset, m_columnNum, m_RecordLength);
// 	if (Ret <= 0 || !m_pColumnInfo || !m_pDataOffset || m_RecordLength<=0)
// 	{
// 		ReleasColumnInfo();
// 		return 0;
// 	}
// 	RlySOENum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(m_AppType, TABLE_RRLYSOEDEFINE_INFO, m_RecordId+PARENT_OFFSET*TABLE_RDEVS_INFO, buff, &buff_len, true);
// 	if (RlySOENum>0 && buff)
// 	{
// 		for (i=0; i<RlySOENum; i++)
// 		{
// 			DevModeRlySOE.ParentId = DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
// 			DevModeRlySOE.IndexNo = DevModexRlySOENum+1;
// 			NameCh = QString::fromLocal8Bit(((rRlySOEDefine_Info*)buff)[i].Name);
// 			NameCh.remove(m_DevName);
// 			sprintf(DevModeRlySOE.NameCh, "%s", NameCh.toLocal8Bit().data());
// 			DevModeRlySOE.Fun = ((rRlySOEDefine_Info*)buff)[i].Fun;
// 			DevModeRlySOE.Inf = ((rRlySOEDefine_Info*)buff)[i].Inf;
// 			DevModeRlySOE.GroupNo = ((rRlySOEDefine_Info*)buff)[i].GroupNo;
// 			DevModeRlySOE.ItemNo = ((rRlySOEDefine_Info*)buff)[i].ItemNo;
// 			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_RLYSOE61850, RecordId);
// 			if (RecordId<=0)
// 			{
// 				break;
// 			}
//
// 			DevModeRlySOE.ID = RecordId;
// 			Retval = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_RLYSOE61850, (char*)&DevModeRlySOE, m_RecordLength, m_pColumnInfo, m_pDataOffset, m_columnNum);
// 			if (Retval<=0)
// 			{
// 				break;
// 			}
// 			DevModexRlySOENum++;
// 		}
// 	}
// 
// 	if (buff)
// 	{
// 		delete [](char*)buff;
// 		buff = NULL;
// 	}
// 
// 	ReleasColumnInfo();
// 	return DevModexRlySOENum;
// }